//
//  ComeStoreGoodsControl.m
//  ai2ban
//
//  Created by Heartbeats on 2018/1/10.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "ComeStoreGoodsControl.h"
#import "JFenColltionCell.h"
#import "FictitiousOrderConfirmControl.h"

@interface ComeStoreGoodsControl ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (strong, nonatomic) UICollectionView *goodsCollectionView;
@property (strong, nonatomic) NSArray<JiFenGoodModel *> *goods;
@end

@implementation ComeStoreGoodsControl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商品";
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(kScreen_Width / 2, kScreen_Width / 2 * 0.8);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    self.goodsCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    self.goodsCollectionView.backgroundColor = [UIColor whiteColor];
    self.goodsCollectionView.dataSource = self;
    self.goodsCollectionView.delegate = self;
    [self.goodsCollectionView registerNib:[UINib nibWithNibName:@"JFenColltionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"JFenCell"];
    [self.view addSubview:self.goodsCollectionView];

    [self loadGoods];
}

- (void)setStoreId:(NSString *)storeId {
    _storeId = storeId;
}

- (void) loadGoods {
    NSDictionary *param = @{@"offset": @"0", @"limit": @"100", @"userId": self.storeId};
    [[NetworkManager shareNetwork] requestWithURLString:@"app/system/getProductlist" parameters:param method:@"" callBack:^(id responseObject) {
        if ([responseObject[@"state"]intValue]) {
            self.goods = [UserModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [self.goodsCollectionView reloadData];
        }
        return;
    }];
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.goods.count;
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    JFenColltionCell *cell = (JFenColltionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"JFenCell" forIndexPath:indexPath];
    cell.model = self.goods[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FictitiousOrderConfirmControl *goodInfo = [[FictitiousOrderConfirmControl alloc] initWithNibName:@"FictitiousOrderConfirmControl" bundle:nil];
    [self.navigationController pushViewController:goodInfo animated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
